//
//  MyCollectViewController.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#import "MyCollectViewController.h"
#import "ItemVC2TableViewCell.h"
#import "ModelForListen.h"
#import "DataBaseHandler.h"
#import "QingKongViewController.h"
#import "ModelForSecendVC.h"
#import "SecendViewController.h"


@interface MyCollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrForCollect;

@end

@implementation MyCollectViewController


- (void)dealloc {

    [_tableView release];
    [super dealloc];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];
    [self.tableView reloadData];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self handleData];


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(handleDelete:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    
    // 返回键设置颜色.
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
}

- (void)viewWillDisappear:(BOOL)animated {

    [self.navigationController setNavigationBarHidden:NO];
}

- (void)handleDelete:(UIBarButtonItem *)btm {

    [[DataBaseHandler shareDataBaseHandler]dropTable];
    [[DataBaseHandler shareDataBaseHandler]closeDB];
    [[DataBaseHandler shareDataBaseHandler]open];
    [[DataBaseHandler shareDataBaseHandler]createTable];
    

    [self handleData];
}

- (void)handleData {

    self.arrForCollect = [NSMutableArray array];
    
    NSArray *arr = [[DataBaseHandler shareDataBaseHandler]selectForModel];
    if (arr.count)
    {
        
    for (ModelForListen *model in arr) {
        [self.arrForCollect addObject:model];
    }
        [self createTableView];
    } else {
    
        NSLog(@"数据库为空.");
        
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kong.jpg"]];
        image.frame = self.view.bounds;
        [self.view addSubview:image];
        [image release];
    }
}


- (void)createTableView {

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[ItemVC2TableViewCell class] forCellReuseIdentifier:@"poolForItemVC2TableViewCell"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrForCollect.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrForCollect objectAtIndex:indexPath.row];
    ItemVC2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForItemVC2TableViewCell"];
    [cell passCollectModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrForCollect objectAtIndex:indexPath.row];
    SecendViewController *secend = [[SecendViewController alloc]init];
    secend.stringForAlbumID = model.albumId;
    secend.modelForListen = model;
    NSLog(@"%@,%@",model,model.albumId);
    
    [self.navigationController pushViewController:secend animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   }



@end
